
package com.ruoyi.screen.service;


import com.ruoyi.screen.dto.*;
import com.ruoyi.screen.entity.ServiceType;
import com.ruoyi.screen.repository.ServiceTypeRepository;
import com.ruoyi.screen.repository.UserProfileRepository;
import com.ruoyi.screen.repository.UserServiceRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

// StatsService.java
@Service
@RequiredArgsConstructor
public class IStatsService {
    private final UserProfileRepository userProfileRepository;
    private final UserServiceRepository userServiceRepository;
    private final ServiceTypeRepository serviceTypeRepository;

    // 1. 服务周使用量
    public Long getWeeklyServiceUsage() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime weekAgo = now.minusWeeks(1);
        return userServiceRepository.countByOpenTimeBetween(weekAgo, now);
    }

    // 2. 服务总使用量
    public Long getTotalServiceUsage() {
        return userServiceRepository.count();
    }

    // 3. 总开通量
    public Long getTotalUserCount() {
        return userProfileRepository.count();
    }



    // 4. 每周内各个服务开通人数
    public List<ServiceUsageDTO> getWeeklyServiceUsageDetails() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime weekAgo = now.minusWeeks(1);
        return userServiceRepository.countServiceUsageByWeek(weekAgo, now);
    }

    // 5. 各个服务的授信额度
    public List<ServiceType> getAllServiceTypes() {
        return serviceTypeRepository.findAll();
    }

    // 6. 各个地区的信用分分布情况
    public List<CreditDistributionDTO> getCreditScoreDistribution() {
        List<Object[]> results = userProfileRepository.calculateCreditScoreDistribution();
        return results.stream().map(row -> {
            CreditDistributionDTO dto = new CreditDistributionDTO();
            dto.setCounty((String) row[0]);
            dto.setMinScore(((Number) row[1]).doubleValue());
            dto.setMaxScore(((Number) row[2]).doubleValue());
            dto.setAvgScore(((Number) row[3]).doubleValue());
            dto.setQ1(((Number) row[4]).doubleValue());
            dto.setQ3(((Number) row[5]).doubleValue());
            return dto;
        }).collect(Collectors.toList());
    }

    // 7. 用户画像
    public Map<String, List<UserProfileDistributionDTO>> getUserProfileDistribution() {
        Map<String, List<UserProfileDistributionDTO>> result = new HashMap<>();
        result.put("creditScore", userProfileRepository.countByCreditScore());
        result.put("age", userProfileRepository.countByAge());
        result.put("gender", userProfileRepository.countByGender());
        result.put("jobType", userProfileRepository.countByJobType());
        return result;
    }

    // 8. 各个地区人群收入情况
    public List<IncomeDistributionDTO> getIncomeDistributionByCounty() {
        return userProfileRepository.getAvgIncomeByCounty();
    }

    // 9. 地区人数分布
    public List<CountyUserCountDTO> getUserCountByCounty() {
        return userProfileRepository.countUsersByCounty();
    }

    // 获取指定服务的每周开通人数
    public List<WeeklyUsageDTO> getWeeklyUsageByService(Integer serviceId) {
        return userServiceRepository.countWeeklyUsageByService(serviceId);
    }
}